package com.king.java.tools.http.client.okhttp.impl;

import com.king.java.tools.http.HttpUtils;
import com.king.java.tools.http.client.okhttp.AbstractOkhttp;
import com.king.java.tools.http.entity.ConnectEntity;
import com.king.java.tools.http.entity.HttpJsonEntity;
import com.king.java.tools.http.entity.HttpMethod;
import com.king.java.tools.utils.StringUtils;
import okhttp3.*;

import java.io.File;

/**
 * 描述:
 *
 * @author : sujinchen
 * @date : 2020/10/16
 */
public class PartHttp extends AbstractOkhttp implements HttpUtils {

    public PartHttp(OkHttpClient client, HttpJsonEntity entity) {
        super(client, entity);
    }

    public PartHttp(HttpJsonEntity entity) {
        super(entity);
        entity.setMethod(HttpMethod.POST);

    }

    @Override
    public ConnectEntity run() throws Exception {
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", entity.getFileName(),
                        RequestBody.create(MediaType.parse("multipart/form-data"),
                                new File(entity.getFilePath())))
                .build();
        Request request = new Request.Builder()
                .url(entity.getUrl())
                .post(requestBody)
                .build();

        return synCall(request);
    }
}
